1743C - Save the Magazines - CodeForces Solution


constructive algorithms dp sortings

Please click on ads to support us..

Python Code:

from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from collections import Counter
from bisect import bisect_left
import sys
from queue import PriorityQueue
import operator as op
from functools import reduce
import re

mod = 1000000007
iinp = lambda: int(sys.stdin.readline())
inp = lambda: sys.stdin.readline().strip()
strl = lambda: list(inp().strip().split(" "))
intl = lambda: list(map(int, inp().split(" ")))
mint = lambda: map(int, inp().split())
flol = lambda: list(map(float, inp().split(" ")))
flush = lambda: sys.stdout.flush()



def myCode():
    num = int(input())
    savem = input()
    array = list(map(int, input().split()))
    box, ss = [0, -2147483647]
    for i in range(0, num):
        if savem[i] == '1':
            box += max(array[i], ss)
        if savem[i] == '0' or array[i] < ss:
            ss = array[i]
    print(box)

def main():
    for t in range(iinp()):
        myCode()


if __name__ == '__main__':
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define fastIO  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
#define max(a,b) (((b)>(a)) ? b : a )
#define min(a,b) (((a)<(b)) ? a : b)
#define endl "\n"
#define space " "

long long binarySearch(int key, int mouse[], int s, int e)
{
    
    while(s<=e)
    {
        int mid=(s+e)/2;

        if (mouse[mid]==key)          
            return mid;
        else if (mouse[mid]>key)      
            e=mid-1;
        else                        
            s=mid+1;
    }
    return -1;

    //O(logn), O(1)
}
long long GCD(int a, int b)
{
    // Everything divides 0
    if (a == 0)
       return b;
    if (b == 0)
       return a;
  
    // base case
    if (a == b)
        return a;
  
    // a is greater
    if (a > b)
        return GCD(a-b, b);
    return GCD(a, b-a);
}

long long LCM(int a, int b)
{
    return a*b/GCD(a,b);
}

//---------------------------------------------------------------------------------------------------//

void solve()
{
    int n;
    cin>>n;

    string lid;
    cin>>lid;
    int arr[n];

    for(int i=0;i<n;i++)
    {
        cin>>arr[i];

    }
    
    int i=n-1;
    while(i>=0)
    {
        if (lid[i]=='0')
        {
            i--;
            
            continue;
        }
        
        int last=i;
        while(i-1>=0 && lid[i-1]=='1' && arr[i-1]>arr[last])
        {
            i--;
        }

        if (i-1>=0 && lid[i-1]=='0' && arr[i-1]>arr[last])
        {
            lid[last]='0';
            lid[i-1]='1';
            i--;
        }

        i--;
    }
    // cout<<lid<<endl;
    int answer=0;
    for(int i=0;i<n;i++)
    {
        if (lid[i]=='1')
        {
            answer+=arr[i];
        }
    }
    cout<<answer<<endl;

}

int main()
{
    fastIO;
    int t;
    cin>>t;
    while(t--)
    {
        solve();


    }
}


Comments

Submit
0 Comments
More Questions

3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret